<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 17:35
 */

namespace app\admin\model;
use think\Model;
use think\Db;
class ApplyModel extends Model{
    // 确定链接表名
    protected $table = 'snake_apply';
    function getAll($map){
        return $this->where($map)
            ->count();
    }

    function getOrder($map,$offset,$limit){
        return $this->where($map)
            ->limit("$offset , $limit")
            ->order('addtime DESC,order_id DESC')
            ->select();
    }
    function pass($id){
        try{
            $order=$this->where(array('order_id'=>$id))->find();
            if($order['status']==1){
                return msg(-1, '', '此订单已审核通过,操作无效');
            }
            $result=true;
            $this->save(array('status'=>1,'check_time'=>time()),['order_id'=>$id]);
            /*
            $user=Db::name('users')->field('vip,vip_time,pay_deposit,deposit')->where(array('user_id'=>$userCash['user_id']))->find();
            $deposit=$user['deposit']-$userCash['money'];
            if($deposit==0){
                $map=array('deposit'=>0,'pay_deposit'=>0);
            }else{
                $map=array('deposit'=>$deposit);
            }
            $result=Db::name('users')->where(array('user_id'=>$userCash['user_id']))->update($map);
            */
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('apply/index'), '审核成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    function reject($id){
        try{
            $order=$this->where(array('order_id'=>$id))->find();
            if($order['status']==1){
                return msg(-1, '', '此订单已审核通过,操作无效');
            }
            $result=true;

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('apply/index'), '已驳回申请');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
}